#!/bin/bash

# 默认参数
HIDE_CONSOLE=0

# 询问用户是否需要隐藏控制台
read -p "是否需要隐藏控制台? (y/n): " answer
if [[ $answer == "y" || $answer == "Y" ]]; then
    HIDE_CONSOLE=1
    echo "将构建无控制台版本..."
else
    echo "将构建标准版本（带控制台）..."
fi

# 检测操作系统
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS 平台
    echo "Building for macOS..."
    
    # 创建构建目录
    mkdir -p build
    cd build
    
    # 编译库文件
    clang -c ../src/mac_webview.m \
        -o mac_webview.o \
        -I../include \
        -framework Cocoa \
        -framework WebKit \
        -fobjc-arc
    
    # 编译 mac_bundle_utils.m (如果需要)
    if [ $HIDE_CONSOLE -eq 1 ]; then
        clang -c ../src/mac_bundle_utils.m \
            -o mac_bundle_utils.o \
            -I../include \
            -framework Foundation \
            -fobjc-arc
        
        # 将 mac_bundle_utils.o 添加到静态库
        ar rcs libwebview.a mac_webview.o mac_bundle_utils.o
    else
        # 创建静态库
        ar rcs libwebview.a mac_webview.o
    fi
    
    # 编译选项
    COMPILE_OPTS="-I../include -L. -lwebview -framework Cocoa -framework WebKit"
    
    # 确保 HTML 文件存在于构建目录
    if [ ! -f "./index.html" ] && [ -f "../build/index.html" ]; then
        echo "复制 HTML 文件到构建目录..."
        cp -f ../build/index.html ./
    fi
    
    # 如果需要隐藏控制台，创建应用程序包
    if [ $HIDE_CONSOLE -eq 1 ]; then
        echo "编译无控制台版本..."
        
        # 添加无控制台编译选项
        COMPILE_OPTS="$COMPILE_OPTS -DHIDE_CONSOLE"
        
        # 创建应用程序包结构
        mkdir -p CustomWindow.app/Contents/MacOS
        mkdir -p CustomWindow.app/Contents/Resources
        
        # 创建 Info.plist
        cat > CustomWindow.app/Contents/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleExecutable</key>
    <string>CustomWindow</string>
    <key>CFBundleIdentifier</key>
    <string>com.example.customwindow</string>
    <key>CFBundleName</key>
    <string>自定义窗口</string>
    <key>CFBundleIconFile</key>
    <string>AppIcon</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>LSMinimumSystemVersion</key>
    <string>10.10</string>
    <key>NSHumanReadableCopyright</key>
    <string>Copyright © 2023. All rights reserved.</string>
    <key>NSPrincipalClass</key>
    <string>NSApplication</string>
    <key>NSMainNibFile</key>
    <string></string>
    <key>LSUIElement</key>
    <true/>
</dict>
</plist>
EOF
        
        # 编译应用程序
        clang ../example/main.c \
            -o CustomWindow.app/Contents/MacOS/CustomWindow \
            $COMPILE_OPTS
        
        # 复制 HTML 文件到资源目录
        echo "复制 HTML 文件到应用程序资源目录..."
        
        # 检查各种可能的 HTML 文件位置
        if [ -f "./index.html" ]; then
            cp -f ./index.html CustomWindow.app/Contents/Resources/
            echo "已复制 ./index.html 到应用程序包"
        elif [ -f "../build/index.html" ]; then
            cp -f ../build/index.html CustomWindow.app/Contents/Resources/
            echo "已复制 ../build/index.html 到应用程序包"
        elif [ -f "../example/index.html" ]; then
            cp -f ../example/index.html CustomWindow.app/Contents/Resources/
            echo "已复制 ../example/index.html 到应用程序包"
        else
            echo "没有 HTML 文件"
        fi
        
        # 验证文件是否已复制
        if [ -f "CustomWindow.app/Contents/Resources/index.html" ]; then
            echo "HTML 文件已成功复制到应用程序包"
            ls -la CustomWindow.app/Contents/Resources/index.html
        else
            echo "错误: HTML 文件复制失败!"
        fi
        
        echo "应用程序包已创建: CustomWindow.app"
        echo "运行方式: open CustomWindow.app"
    else
        # 编译标准版本
        clang ../example/main.c \
            -o example \
            $COMPILE_OPTS
        
        echo "标准版本已编译: example"
        echo "运行方式: ./example"
    fi
    
    echo "构建完成。"
    
else
    echo "此脚本仅支持 macOS 平台"
    exit 1
fi 